package com.sosee.cgfw.friends;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sosee.cgfw.R;
import com.sosee.cgfw.entity.Data;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.LogUtils;
import com.sosee.cgfw.utils.Utils;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Thinkpad on 2016/3/13.
 */
public class FriendsGiveShowCard extends Activity implements View.OnClickListener {

    private RelativeLayout rl;
    List<Integer> plist=new ArrayList<Integer>();
    private int margin;
    private int total=5;
    private int size=4;
    private int height;
    private Map<Integer,View> map=new HashMap<Integer, View>();
    private List<Integer> posList=new ArrayList<Integer>();//用于记录view 的初始位置  方便计算 偏移量
    private boolean isZhankai=false;//判断 是否已经有 展开项 view
    private HttpUtils httpUtils=new HttpUtils();
    private View v1;
    private YaoqinHanData yaoqinHanData;
    private  List<YaoqinHanData.Result> result;
    private List<YaoqinHanData.Result> integralBean;

    private List<Boolean> zlist=new ArrayList<Boolean>();//记录 各个view的展开状态

    String time="2015-10-30";
    private List<View> vlist=new ArrayList<View>();

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:

                    integralBean = (List<YaoqinHanData.Result>) msg.obj;


                    break;

                default:
                    break;
            }
        };
    };


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friends_give_showcard);

        initData_web();
        init();
        initView();
        initData();

    }

    private void init() {

            margin=getWindowManager().getDefaultDisplay().getHeight()/size;
            height=(int)(getWindowManager().getDefaultDisplay().getHeight()/1.3f);
            LogUtils.log_E("height" + height);

    }
    public void initView(){
        vlist.clear();
        rl= (RelativeLayout) findViewById(R.id.lll);
        for (int i=0;i<integralBean.get(i).getData().getShow().size();i++){
            View v1=View.inflate(this,R.layout.item_childviews,null);
            RelativeLayout.LayoutParams params1=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,height);
            params1.topMargin=margin*i;
            v1.setLayoutParams(params1);
            v1.setId(i);
            v1.setOnClickListener(this);

            setViewData(v1,i);

            vlist.add(v1);
            rl.addView(v1);
            plist.add(i);
            posList.add(params1.topMargin);
            LogUtils.log_E("初始位置：" + params1.topMargin);
            zlist.add(false);
            map.put(i, v1);
        }


    }


    //给 每个view 设置数据
    public void setViewData(View v, int position){
        TextView tv_start_time= (TextView) v.findViewById(R.id.tv_start_time_s);//开场时间
        TextView tv_title= (TextView) v.findViewById(R.id.tv_title_s);
        TextView tv_company= (TextView) v.findViewById(R.id.tv_company_s);
        TextView tv_company_e= (TextView) v.findViewById(R.id.tv_company_e_s);
        TextView tv_date= (TextView) v.findViewById(R.id.tv_date_s);
        TextView tv_time= (TextView) v.findViewById(R.id.tv_time_s);
        TextView tv_address= (TextView) v.findViewById(R.id.tv_address_s);
        TextView tv_address_e= (TextView) v.findViewById(R.id.tv_address_e_s);

        YaoqinHanData.Result data=result.get(position);
        tv_title.setText(data.getData().getShow().get(position).getData1().getTitle());
        tv_company.setText(data.getData().getShow().get(position).getData1().getUndertake());
        tv_company_e.setText(data.getData().getShow().get(position).getData1().getUndertake_e());
        tv_date.setText(data.getData().getShow().get(position).getData1().getStart_date());
        tv_time.setText(data.getData().getShow().get(position).getData1().getEnd_date());
        tv_address.setText(data.getData().getShow().get(position).getData1().getAddressA());
        tv_address_e.setText(data.getData().getShow().get(position).getData1().getAddressA_e());
        tv_start_time.setText(data.getData().getShow().get(position).getData1().getStart_time());
        final String showID=data.getData().getShow().get(position).getData1().getID();



    }

    public void initData(){

        for (int i=1;i<posList.size();i++){
            int a=posList.get(i)+height;

            ylist.add(a);
            LogUtils.log_E("默认位置："+a);
            LogUtils.log_E("ylist默认："+ylist.get(i-1));
        }
    }

    private List<Data> tlist=new ArrayList<Data>();
    private List<String> erList=new ArrayList<String>();
    public void initData_web(){

        httpUtils.configCurrentHttpCacheExpiry(1000 * 1);
        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.PIAO_URL + Constans.userID, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                System.out.println("这是邀请函：" + responseInfo.result);
                if (null != responseInfo.result) {
                    Gson gson = new Gson();
                    yaoqinHanData = gson.fromJson(responseInfo.result, YaoqinHanData.class);
                    if ("success".equals(yaoqinHanData.getStatus())){
                        result = yaoqinHanData.getResult();
//                        total=result.size();


                        Message message = Message.obtain();
                        message.obj = result;
                        message.what = 1;
                        handler.sendMessage(message);

                    }
                }


            }

            @Override
            public void onFailure(HttpException e, String s) {
                Utils.showToast("网络异常" + s);
            }
        });
    }


    //刷新列表
    private List<Integer> ylist=new ArrayList<Integer>();
    public void animation_zhankai(int id,View v,List<Integer> plist){
        plist.remove((Integer) id);
        for (int i=0;i<plist.size();i++){
            final View mView=map.get(plist.get(i));
            if(plist.get(i)!=id){

//                final int log=height-margin;
                final int pos=ylist.get(i);
                final int
                        log=ylist.get(i)-(int)mView.getY()-margin;
                TranslateAnimation tr=new TranslateAnimation(mView.getX(),mView.getX(),0,log);
                tr.setDuration(1000);
                tr.setInterpolator(new AccelerateDecelerateInterpolator());
                tr.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        mView.clearAnimation();
                        int y=pos-margin;
                        RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,height);
                        params.topMargin=y;
                        mView.setLayoutParams(params);
                    }
                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                mView.startAnimation(tr);
            }else{
                int pianyi=(int)v.getY();
                int log=0-pianyi;
                TranslateAnimation tr=new TranslateAnimation(mView.getX(),0,0,log);
                LogUtils.log_E("xiangshang:"+mView.getY());
                tr.setDuration(1000);
                tr.setInterpolator(new AccelerateDecelerateInterpolator());
                tr.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        mView.clearAnimation();
                        int y = 0;
                        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, height);
                        params.topMargin = y;
                        mView.setLayoutParams(params);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                mView.startAnimation(tr);
            }

        }
    }

    //展开指定id 的view视图
    public void animation_zhankai_id(int zID){
        {
            final View mView=map.get(plist.get(zID));
            final int log = 0-(int)mView.getY();
            TranslateAnimation tr=new TranslateAnimation(mView.getX(),mView.getX(),0,log);
            tr.setDuration(1000);
            tr.setInterpolator(new AccelerateDecelerateInterpolator());
            tr.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }
                @Override
                public void onAnimationEnd(Animation animation) {
                    mView.clearAnimation();
                    int y=0;
                    RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,height);
                    params.topMargin=y;
                    mView.setLayoutParams(params);
                }
                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            mView.startAnimation(tr);}



    }


    @Override
    public void onClick(View v) {


        int id=v.getId();
//                if(!isZhankai){
//                    animation_zhankai(id,v);
//                    isZhankai=true;
//                }else{
//                    animation_shouhui(id);
//                    isZhankai=false;
//                }

//        doAnimation(v);
        List<Integer> temlist=new ArrayList<Integer>();
        temlist.addAll(plist);
        animation_zhankai(id, v, temlist);
        animation_zhankai_id(v.getId());
    }


}
